package com.ltkj.ticket.service.impl;

import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.system.api.RemoteFileService;
import com.ltkj.system.api.domain.CommAttachment;
import com.ltkj.system.api.model.LoginUser;
import com.ltkj.ticket.domain.TicOtherBase;
import com.ltkj.ticket.domain.TicVenues;
import com.ltkj.ticket.domain.TicVenuesPeriod;
import com.ltkj.ticket.domain.dto.TicVenuesFrontQueryDTO;
import com.ltkj.ticket.domain.vo.TicVenuesFrontDetailTimeVO;
import com.ltkj.ticket.domain.vo.TicVenuesFrontDetailVO;
import com.ltkj.ticket.domain.vo.TicVenuesFrontQueryVO;
import com.ltkj.ticket.domain.vo.TicVenuesPeriodFrontListVO;
import com.ltkj.ticket.mapper.TicOtherBaseMapper;
import com.ltkj.ticket.mapper.TicVenuesFrontMapper;
import com.ltkj.ticket.mapper.TicVenuesMapper;
import com.ltkj.ticket.mapper.TicVenuesPeriodMapper;
import com.ltkj.ticket.service.ITicVenuesFrontService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author gushaoliang
 * @Date 2023/9/1 9:42
 * @PackageName:com.ltkj.ticket.service.impl
 * @ClassName: VenuesFronServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class TicVenuesFrontServiceImpl implements ITicVenuesFrontService {
    private static final Logger log = LoggerFactory.getLogger(TicVenuesFrontServiceImpl.class);
    @Autowired
    private TicVenuesFrontMapper ticVenuesFrontMapper;
    @Autowired
    private TicOtherBaseMapper tcOtherBaseMapper;
    @Autowired
    private TicVenuesPeriodMapper ticVenuesPeriodMapper;
    @Autowired
    private RemoteFileService remoteFileService;
    @Autowired
    private TicVenuesMapper ticVenuesMapper;

    @Override
    public List<TicVenuesFrontQueryVO> searchVenuesFront(TicVenuesFrontQueryDTO ticVenuesFrontQueryDTO) {
        List<TicVenuesFrontQueryVO> ticVenuesFrontQueryVOS = ticVenuesFrontMapper.searchVenuesFront(ticVenuesFrontQueryDTO);
        if (ticVenuesFrontQueryVOS != null && ticVenuesFrontQueryVOS.size() > 0) {
            ticVenuesFrontQueryVOS.stream().forEach(x -> {
                // 获取当日可预约的时间段
                List<String> strings = ticVenuesFrontMapper.searchVenuesFrontTimeList(x.getTicOtherBaseId());
                x.setTimeList(strings);
            });

        }
        return ticVenuesFrontQueryVOS;
    }

    @Override
    public TicVenuesFrontDetailVO searchVenuesFrontDetail(TicVenuesFrontQueryDTO ticVenuesFrontQueryDTO) {
        TicVenuesFrontDetailVO ticVenuesFrontDetailVO = new TicVenuesFrontDetailVO();







        // 获取场馆信息
        TicOtherBase ticOtherBase = tcOtherBaseMapper.selectTicOtherBaseByTicOtherId(ticVenuesFrontQueryDTO.getTicOtherBaseId());
        ticVenuesFrontDetailVO.setTicOtherBaseId(ticOtherBase.getTicOtherId());
        ticVenuesFrontDetailVO.setName(ticOtherBase.getName());
        ticVenuesFrontDetailVO.setLatitude(ticOtherBase.getLatitude());
        ticVenuesFrontDetailVO.setLongitude(ticOtherBase.getLongitude());
        ticVenuesFrontDetailVO.setAddressDetails(ticOtherBase.getAddressDetails());
        ticVenuesFrontDetailVO.setIntroduceDetails(ticOtherBase.getIntroduceDetails());
        ticVenuesFrontDetailVO.setTouristNotice(ticOtherBase.getTouristNotice());

            // 获取门票信息
        TicVenues ticVenues1 = ticVenuesFrontMapper.queryByOtherBaseId(ticVenuesFrontQueryDTO.getTicOtherBaseId());
        if(ticVenues1!=null){
            ticVenuesFrontDetailVO.setVenuesPhone(ticVenues1.getVenuesPhone());
            ticVenuesFrontDetailVO.setLimitPurchaseNum(ticVenues1.getLimitPurchaseNum());
            ticVenuesFrontDetailVO.setScenicAuth(ticVenues1.getScenicAuth());
            ticVenuesFrontDetailVO.setVenuesId(ticVenues1.getVenuesId());

        }
        List<CommAttachment> commAttachmentImgList = remoteFileService.selectAtta(ticOtherBase.getTicOtherId(), "tic_other_base_img");
        ticVenuesFrontDetailVO.setImgList(commAttachmentImgList);
        // TicVenues ticVenues = ticVenuesFrontMapper.searchVenuesFrontByBaseId(ticVenuesFrontQueryDTO.getTicOtherBaseId(), ticVenuesFrontQueryDTO.getVenuesDate());
        // if (ticVenues != null) {
        //     ticVenuesFrontDetailVO.setLimitPurchaseNum(ticVenues.getLimitPurchaseNum());
        //     ticVenuesFrontDetailVO.setScenicAuth(ticVenues.getScenicAuth());
        //
        // }
        //查询此场馆最近一天有票的数据
        //Date date = ticVenuesFrontMapper.searchVenuesFrontByBaseIdDate(ticVenuesFrontQueryDTO.getTicOtherBaseId());//

        // if(date==null){
        //     return ticVenuesFrontDetailVO;
        // }
        // if (ticVenuesFrontQueryDTO.getVenuesDate() == null || ticVenuesFrontQueryDTO.getVenuesDate().equals("")) {
        //     ticVenuesFrontQueryDTO.setVenuesDate(date);
        // }
        List<String> dateListS = ticVenuesFrontMapper.searchVenuesFrontByBaseIdDateV2(ticVenuesFrontQueryDTO.getTicOtherBaseId());
        if(dateListS==null||dateListS.size()==0){
            return ticVenuesFrontDetailVO;
        }
        if (ticVenuesFrontQueryDTO.getVenuesDate() == null || ticVenuesFrontQueryDTO.getVenuesDate().equals("")) {
            Date date = DateUtils.parseDate(dateListS.get(0));
            ticVenuesFrontQueryDTO.setVenuesDate(date);
            }

        // 获取场馆生成的日期(暂定为未开7天)
        List<TicVenuesFrontDetailTimeVO> dateList = new ArrayList<>();
        // List<String> sevenDate = DateUtils.getSevenDate(date);
        dateListS.stream().forEach(x -> {
            TicVenuesFrontDetailTimeVO ticVenuesFrontDetailTimeVO = new TicVenuesFrontDetailTimeVO();
            ticVenuesFrontDetailTimeVO.setVenuesDate(x);
            String[] split = x.split("-");
            int month = Integer.parseInt(split[1]);
            String day = split[2];
            ticVenuesFrontDetailTimeVO.setMonth(month + "月");
            ticVenuesFrontDetailTimeVO.setDay(day + "");
            // 判断当前是星期几
            ticVenuesFrontDetailTimeVO.setWeekDays(DateUtils.getWeekOfDate(x));
            dateList.add(ticVenuesFrontDetailTimeVO);
        });
        ticVenuesFrontDetailVO.setTicVenuesFrontDetailTimeList(dateList);
        // 获取日期相对于的时间段信息
        TicVenuesPeriod ticVenuesPeriod = new TicVenuesPeriod();
        ticVenuesPeriod.setTicOtherBaseId(ticVenuesFrontQueryDTO.getTicOtherBaseId());
        ticVenuesPeriod.setVenuesDate(ticVenuesFrontQueryDTO.getVenuesDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<TicVenuesPeriodFrontListVO> ticVenuesPeriodFrontListVOS = new ArrayList<>();
        if(loginUser!=null){
            ticVenuesPeriodFrontListVOS = ticVenuesFrontMapper.
                    searchTicVenuesPeriodFrontListLogin(ticVenuesFrontQueryDTO.getTicOtherBaseId(), ticVenuesFrontQueryDTO.getVenuesDate(), loginUser.getUserid());
        }else {
            ticVenuesPeriodFrontListVOS = ticVenuesFrontMapper.
                    searchTicVenuesPeriodFrontList(ticVenuesFrontQueryDTO.getTicOtherBaseId(), ticVenuesFrontQueryDTO.getVenuesDate());
        }



        //循环查询当前用户是否存在当前时间点段订单

        ticVenuesFrontDetailVO.setTicVenuesPeriodFrontList(ticVenuesPeriodFrontListVOS);

        return ticVenuesFrontDetailVO;
    }


}